package org.wanho.shop.dao.impl;

import org.wanho.jdbc.JdbcTemplate;
import org.wanho.shop.dao.OrderDao;
import org.wanho.shop.entity.Order;
import org.wanho.shop.mapper.OrderMapper;
import org.wanho.util.RowMapper;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-04-27 9:58
 * Description：<描述>
 */
public class OrderDaoImpl implements OrderDao {

    private JdbcTemplate<Order> jt = new JdbcTemplate<>();
    private RowMapper<Order> rm = new OrderMapper();

    @Override
    public int insert(Order order) {
        String sql = new StringBuffer()
                .append(" insert into t_order ")
                .append("   (no,price,user_id) ")
                .append(" values ")
                .append("   (?,?,?) ")
                .toString();
        return jt.save(sql,order.getNo(),order.getPrice(),order.getUser().getId());
    }

    @Override
    public List<Order> selectByUserId(Integer userId) {
        String sql = new StringBuffer()
                .append(" select ")
                .append("   id,no,price,user_id ")
                .append(" from ")
                .append("   t_order ")
                .append(" where ")
                .append("   user_id=? ")
                .toString();
        return jt.query(sql,rm,userId);
    }

    @Override
    public Order selectDetailById(int id) {
        String sql = new StringBuffer()
                .append(" select ")
                .append("     id,no,price,user_id ")
                .append(" from ")
                .append("   t_order ")
                .append(" where ")
                .append("   id = ? ")
                .toString();
        return jt.queryForObject(sql, rm, id);
    }
}
